﻿using MediatR;
using SystemLYH.API.Application.Command.ReservoirCommand;
using SystemLYH.Domain.Dto;
using SystemLYH.Domain;
using SystemLYH.Infrstructre.Interface;

namespace SystemLYH.API.Application.Handlers.ReservoirHandlers
{
    public class ParticularsShowReservoirCommandHandler:IRequestHandler<ParticularsShowReservoirCommand, PageDTO<Reservoir>>
    {
        protected readonly IReservoirRepository _reservoirRepository;
        /// <summary>
        /// 库区构造函数
        /// </summary>
        /// <param name="reservoirRepository"></param>
        public ParticularsShowReservoirCommandHandler(IReservoirRepository reservoirRepository)
        {
            _reservoirRepository = reservoirRepository;
        }

        public async Task<PageDTO<Reservoir>> Handle(ParticularsShowReservoirCommand request, CancellationToken cancellationToken)
        {
            PageDTO<Reservoir> page = new PageDTO<Reservoir>();
            var list = (await _reservoirRepository.GetAllAsync()).Where(x => !x.IsDelReservoir);
           list = list.Where(x => x.WarehouseID == request.WarehouseID);
            page.pageRoot = list.Count();
            page.pageCount = (int)Math.Ceiling(page.pageRoot * 1.0 / request.pageSize);
            page.plist = list.OrderByDescending(x => x.ReservoirId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return page;
        }
    }
}
